package com.tao.controller;

import com.alibaba.fastjson.JSON;
import com.tao.dto.UserDto;
import com.tao.entity.LoginUser;
import com.tao.entity.User;
import com.tao.service.UserService;
import com.tao.utils.BeanCopyUtils;
import com.tao.utils.SecurityUtils;
import com.tao.utils.UploadUtils;
import com.tao.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 季伟
 * @date 2023/5/18 11:49
 * 功能：
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/userInfo")
    public ResponseResult getUserInfo(){
        Long userId = SecurityUtils.getUserId();
        User user = userService.getByUserId(userId);
        return ResponseResult.okResult(user);
    }
    @GetMapping("/userInfo/byId/{id}")
    public ResponseResult getUserInfoById(@PathVariable Long id){
        return userService.getUserInfoById(id);
    }
    


    @PutMapping("/userInfo")
    public ResponseResult updateUserInfo(UserDto userDto) {
        String avatarUrl = null;
        if(userDto.getAvatarFile() != null && !userDto.getAvatarFile().isEmpty()){
            avatarUrl = UploadUtils.uploadAvatar(userDto.getAvatarFile());
        }
        System.out.println(avatarUrl);
        Long userId = SecurityUtils.getUserId();
        User user = BeanCopyUtils.copyBean(userDto,User.class);
        user.setId(userId);
        user.setAvatar(avatarUrl);
        userService.updateById(user);
        return ResponseResult.okResult();
    }
    @PostMapping("register")
    public ResponseResult register(@RequestBody User user){
        return userService.register(user);
    }

}
